/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the Uno and
  Leonardo, it is attached to digital pin 13. If you're unsure what
  pin the on-board LED is connected to on your Arduino model, check
  the documentation at http://www.arduino.cc

  This example code is in the public domain.

  modified 8 May 2014
  by Scott Fitzgerald
 */

#define ledR (12)
#define ledG (11)
#define ledBrd (13)
#define sw1 (2)
#define SW_ON  (0)
#define SW_OFF  (1)

#define LED_ON  (1)
#define LED_OFF  (0)
#define interval  (500)
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

//现在状态
unsigned char swState;
//过去状态
unsigned char swPrevState;
unsigned char ledState;
unsigned char ledBlinkState;
// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(ledBrd, OUTPUT);
  pinMode(ledR, OUTPUT);
  pinMode(ledG, OUTPUT);
  pinMode(sw1, INPUT);
  
  digitalWrite(ledR,LOW);
  digitalWrite(ledG,LOW);
  swState=swPrevState=digitalRead(sw1);
  ledState=LED_OFF;
  ledBlinkState=LOW;
}

void ledAllOff()
{
  digitalWrite(ledR,HIGH);
  digitalWrite(ledG,HIGH);
}

int a = -1
int i = 0;
// the loop function runs over and over again forever
void loop() {
   swState=digitalRead(sw1);
  if(swState == LOW)
    {
      delay(1500);
      if(swState == LOW)
         i = 1;
    }
   else if(swState ==SW_ON && swPrevState==SW_OFF)
   {
     i = 0;
     a++;
    }
    if(i == 0)
    {
      a = a % 4;
      if(a == 0)
      {
        digitalWrite(ledR, HIGH);
        digitalWrite(ledG, LOW);
      }
      else if( a == 1)
      {
        digitalWrite(ledR, LOW);
        digitalWrite(ledG, HIGH);
       }
     else if(a  == 2)
         blink();
     else if( a == 3 )
         blinkWithoutDelay();
     swPrevState = swState; 
    }
    else if(i == 1)
    {
      digitalWrite(ledG,LOW);
      digitalWrite(ledR,LOW);
      swState=swPrevState;
      ledState=LED_OFF;
      ledBlinkState=LOW;
      a = -1;
    }
}

void blink()
{
  digitalWrite(ledR, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(ledG,LOW);

  delay(500);              // wait for a second
  digitalWrite(ledR, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(ledG,HIGH);
  delay(500);              // wait for a second
  
}

void blinkWithoutDelay()
{
    unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledBlinkState == LOW)
    {
      ledBlinkState = HIGH;
    }else{
      ledBlinkState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledG, ledBlinkState);   // turn the LED on (HIGH is the voltage level)
    digitalWrite(ledR,ledBlinkState);
  }

}

